#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdatomic.h>

atomic_long cnt = 0L;

void *count(void *vargp) {
    long i, n = *((long *)vargp);
    for (i = 0; i < n; ++i) {
        cnt++;
    }
    return NULL;
}


int main(int argc, char **argv) {
    long n;
    pthread_t tid1, tid2;

    if (argc != 2) {
        printf("usage: %s <n> \n", argv[0]);
        exit(0);
    }

    n = atoi(argv[1]);

    pthread_create(&tid1, NULL, count, &n);
    pthread_create(&tid2, NULL, count, &n);

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    if (cnt != (2 * n)) {
        printf("BOOM！cnt = %ld\n", cnt);
    } else {
        printf("OK！cnt = %ld\n", cnt);
    }

    exit(0);
}
